package com.flowerplus.store.common.data.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;

import java.lang.reflect.Type;

import javax.inject.Inject;

/**
 * Created by ems on 2017/2/28.
 */

public class JsonUtil {
    Gson mGson;
    private static final JsonUtil sJsonUtil = new JsonUtil();

    @Inject
    public JsonUtil() {
        mGson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
    }

    public static JsonUtil getInstance() {
        return sJsonUtil;
    }

    public String toJson(Object o) {
        return mGson.toJson(o);
    }

    public <T> T fromJson(String json, Class<?> classOfT) {
        return (T) mGson.fromJson(json, classOfT);
    }
    public <T> T fromJson(JsonReader json, Class<?> classOfT) {
        return (T) mGson.fromJson(json, classOfT);
    }

    public <T> T fromJson(String json, Type typeOfT) {
        return mGson.fromJson(json, typeOfT);
    }
}
